package com.pctx.bibadevicemanager.service.impl;

import com.pctx.bibacommon.result.Result;
import com.pctx.bibacommon.util.DateTimeUtil;
import com.pctx.bibacommon.util.UuidUtil;
import com.pctx.bibadevicemanager.dao.TrackerSharePermissionMapper;
import com.pctx.bibadevicemanager.pojo.TrackerSharePermission;
import com.pctx.bibadevicemanager.pojo.TrackerSharePermissionExample;
import com.pctx.bibadevicemanager.service.TrackerSharePermissionService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by chason on 2018/3/12.16:14
 */
@Service("trackerSharePermissionService")
public class TrackerSharePermissionServiceImpl implements TrackerSharePermissionService {
    @Autowired
    private TrackerSharePermissionMapper trackerSharePermissionMapper;

    /**
     * 添加默认权限
     *
     * @param deviceUserUuid
     * @param deviceUuid
     * @param userUuid
     * @return
     * @throws Exception
     */
    @Override
    public boolean addDefaultPermission(String deviceUserUuid, String deviceUuid, String userUuid) throws Exception {
        if (StringUtils.isEmpty(deviceUserUuid) || StringUtils.isEmpty(deviceUuid) || StringUtils.isEmpty(userUuid))
            return false;

        TrackerSharePermission trackerSharePermission = new TrackerSharePermission();
        trackerSharePermission.setUuid(UuidUtil.randomUUID());
        trackerSharePermission.setDeviceUserUuid(deviceUserUuid);
        trackerSharePermission.setDeviceUuid(deviceUuid);
        trackerSharePermission.setUserUuid(userUuid);
        trackerSharePermission.setPositionFlag(TrackerSharePermission.FLAG_ON);
        trackerSharePermission.setCreateTime(DateTimeUtil.nowDate());
        trackerSharePermission.setUpdateTime(DateTimeUtil.nowDate());

        return trackerSharePermissionMapper.insertSelective(trackerSharePermission) > 0;
    }

    /**
     * 修改权限
     *
     * @param trackerSharePermission
     * @return
     * @throws Exception
     */
    @Override
    public boolean updateByUuid(TrackerSharePermission trackerSharePermission) throws Exception {
        if (null == trackerSharePermission || StringUtils.isEmpty(trackerSharePermission.getUuid())) {
            return false;
        }

        TrackerSharePermission old = getByUuid(trackerSharePermission.getUuid());
        if (null == old) {
            return false;
        }

        trackerSharePermission.setId(old.getId());

        return trackerSharePermissionMapper.updateByPrimaryKeySelective(trackerSharePermission) > 0;
    }

    @Override
    public boolean updateByPrimaryKey(TrackerSharePermission trackerSharePermission) throws Exception {
        if (null == trackerSharePermission.getId()) {
            return false;
        }

        return trackerSharePermissionMapper.updateByPrimaryKeySelective(trackerSharePermission) > 0;
    }

    @Override
    public TrackerSharePermission getByUuid(String uuid) throws Exception {
        if (StringUtils.isEmpty(uuid))
            return null;

        TrackerSharePermissionExample example = new TrackerSharePermissionExample();
        TrackerSharePermissionExample.Criteria criteria = example.createCriteria();
        criteria.andUuidEqualTo(uuid).andStatusNotEqualTo(TrackerSharePermission.STATUS_DEL);

        List<TrackerSharePermission> list = trackerSharePermissionMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list))
            return null;

        return list.get(0);
    }

    @Override
    public Result updateTrackerPermission(String uuid, Byte trackerFlag, Byte fenceFlag) throws Exception {
        if (StringUtils.isEmpty(uuid)) {
            return Result.error("参数不能为空");
        }

        TrackerSharePermission trackerSharePermission = getByUuid(uuid);
        if (null == trackerSharePermission) {
            return Result.error("无关联关系");
        }

        trackerSharePermission.setTrackerFlag(trackerFlag);
        trackerSharePermission.setFenceFlag(fenceFlag);

        boolean flag = updateByPrimaryKey(trackerSharePermission);

        if (flag) {
            return Result.success("修改成功");
        } else {
            return Result.error("修改失败");
        }
    }
}
